define(['jquery', 'bootstrap', 'utilback', 'table', 'form', 'ztree'], function ($, undefined, Utilback, Table, Form) {

	var Controller = {
    	config: {
    		get_user: '/json/person.json',
            add_url:'userEdit.html',
    		// get_role: '/role/read/page',
    		// get_menu: '/menu/read/list',
    		// get_promission: '/menu/read/tree'
    	},
        init: function () {
            // 初始化表格参数配置
            Table.api.init({
            	singleSelect: true,
            });
            
            // 清理缓存
            $('.btn-refresh-cache').on('click', function() {
            	Util.api.ajax({
            		type: 'POST',
            		url: '/cache/update',
            		data: JSON.stringify({key: 'Permission'})
            	});
            });
            
           this.initUserRole();
        },
        initUserRole: function () {
            var userTable = $("#userTable");
            // 初始化表格
            userTable.bootstrapTable({
            	toolbar: '#toolbar1',
            	showExport: false,
                url: Controller.config.get_user,
                method:'post',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'workShop', title: '车间'},
                        {field: 'temgroup', title: "班组"},
                        {field: 'userName', title: "姓名"},
                        {field: 'idCard', title: "身份证号"},
                        {field: 'driverType', title: "司机类型"},
                        {field: 'licenceNum', title: "驾驶证号"},
                        {field: 'degreeCulture', title: "文化程度"},
                        {field: 'learnDriveNum', title: "学习司机证"},
                        {field: 'driveCard', title: "司机证"},
                        {field: 'validTime', title: "有效时间"},
                        {field: 'skillRange', title: "技能等级"},
                        {field: 'certificate', title: "高铁合格证"},
                        {field: 'phone', title: "联系电话"},
                        {field: 'carNum', title: "车号"},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(userTable);
            
   
            
            // 根据用户查询用户角色
            userTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table fa.event.check', function () {
            	userRoleTable.bootstrapTable('uncheckAll');
                var ids = Table.api.selectedids(userTable);
                if(ids.length) {
                	Util.api.ajax({
                		type: 'PUT',
                		url: '/user/read/role',
            			data : JSON.stringify({userId : ids[0]}),
                	}, function(result) {
                		var roleIds = [];
                		if (result.code == 200) {
                			var rows = result.rows;
        					for(var i=0; i<rows.length;i++) {
        						roleIds.push(rows[i].roleId);
        					}
        				}
                		userRoleTable.bootstrapTable("checkBy", {field:"id", values: roleIds});
                	});
                }
            });
            
            // 保存用户角色
            $('#userRole').on('click', '.btn-save', function() {
            	var userIds = Table.api.selectedids(userTable);
            	var roleIds = Table.api.selectedids(userRoleTable);
            	var data = [ {userId: userIds[0], permission: 'read'} ];
            	$.each(roleIds, function(i, roleId) {
        			data.push({userId: userIds[0], roleId: roleId, permission: 'read'});
        		});
            	if(userIds.length && roleIds.length) {
                	Util.api.ajax({
                		type: 'POST',
                		url: '/user/update/role',
            			data : JSON.stringify(data),
                	}, function(result) {
                		Toastr.success('保存成功.');
                	});
                }
            });
        },
       
    };
    return Controller;
});